package com.t123.exception;


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;


/***
 * 只要用这个注解,就可以捕获controller异常
 * @RestControllerAdvice 以json的方式返回数据
 * @ControllerAdvice  以String的方式返回数据
 */


@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ArrayIndexOutOfBoundsException.class)
    public Map<String,Object> ArrayIndexOutOfBoundsException(){
        Map<String,Object> map=new HashMap();
        map.put("code",999);
        map.put("massage","数组越界");
        return map;
    }


    @ExceptionHandler(value = NullPointerException.class)
    public Map<String,Object> NullPointerException(){
        Map<String,Object> map=new HashMap();
        map.put("code",999);
        map.put("massage","空指针异常");
        return map;
    }
}
